package com.gkey.sole.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.gkey.core.dao.impl.EntityDaoImpl;
import com.gkey.sole.base.bean.CMenu;
import com.gkey.sole.base.bean.TreeModel;
import com.gkey.sole.base.service.CMenuService;


@Service
public class CMenuServiceImpl extends EntityDaoImpl<CMenu, Integer> implements CMenuService {

	@Override
	public List<TreeModel> getCmenuByLevel(Integer level) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeModel> getCmenuByfatherid(Integer fatherid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeModel> getCmenuByfatherid(Integer fatherid, Integer usercode, Integer level) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CMenu> getCmenuByPcode(Integer fatherid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CMenu getLastMenuByPcode(Integer fatherid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CMenu getLastMenuNext(Integer fatherid, Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer saveMenu(CMenu cMenu) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer updateMenu(CMenu cMenu) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean delMenu(Integer id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public CMenu getMenu(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CMenu> findMenuByUserId(Integer userId) {
		
		List<CMenu> menulist = this.findMenuByUserIdOne(-1, userId);//1、先查找个人权限，有即返回
		if(menulist.size()>0){
			return menulist;
		}
		
		menulist = this.findMenuByUserIdTwo(-1, userId);//1、没有个人权限，去查找角色权限，有即返回
		if(menulist.size()>0){
			return menulist;
		}
		
		//什么都没有返回null
		return null;
	}

	
	
	@Override
	public List<CMenu> findMenuByUserIdOne(Integer fatherId,Integer userId) {		
		List<CMenu> resltmenus = new ArrayList<CMenu>();		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("fatherid", fatherId);
		map.put("userid", userId);
		
		List<CMenu> menulist = super.find("findMenuByUserIdOne", map);
		
		for (CMenu cMenu : menulist) {
			
			cMenu.setChildren(findMenuByUserIdOne(cMenu.getId(), userId));
			
			resltmenus.add(cMenu);
		}
		
		return resltmenus;
	}

	@Override
	public List<CMenu> findMenuByUserIdTwo(Integer fatherId,Integer userId) {
		List<CMenu> resltmenus = new ArrayList<CMenu>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("fatherid", fatherId);
		map.put("userid", userId);
		
		List<CMenu> menulist = super.find("findMenuByUserIdTwo", map);
		
		for (CMenu cMenu : menulist) {
			
			cMenu.setChildren(findMenuByUserIdTwo(cMenu.getId(), userId));
			
			resltmenus.add(cMenu);
		}
		
		return resltmenus;
	}

	@Override
	public List<CMenu> findMenuIds(String menuids) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
}
